package com.yifi.system.controller;

import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import com.yifi.common.DBHelper;
import com.yifi.common.SysUtil;
import com.yifi.common.base.BaseController;
import com.yifi.common.utils.DateUtils;
import com.yifi.common.utils.DbUtils;
import com.yifi.common.utils.ToolSession;
import com.yifi.common.utils.ToolString;
import com.yifi.system.service.SysDeptService;
import com.yifi.system.service.UploadFileService;

import cn.hutool.core.util.StrUtil;

/**
 * @ClassName: SysOrgInfoController
 * @Description: TODO
 * @author 姚善利
 * @date 2021-10-27 17:23:25
 * 
 */
@Path(value = "/sysorginfo", viewPath = "/")
public class SysOrgInfoController extends BaseController {

	public void index() {
		String path = getRequest().getContextPath();
		String webServer = getRequest().getScheme() + "://" + getRequest().getServerName() + ":"
				+ getRequest().getServerPort() + path;
		;
		set("webServer", webServer);

		render("system/sysorginfo.html");
	}

	public void list() {
		String sqlSelect = "select * ";
		String sqlExcept = " from org_info ";

		renderJson(getDataTable(sqlSelect, sqlExcept));
	}

	// 新增
	public void add() {
		String ccode = getPara("ccode", "");
		set("ccode", ccode);
		String ctype = getPara("ctype", "");
		set("ctype", ctype);

		render("system/sysorginfo_edit.html");
	}

	// 编辑
	public void edit() {
		String ccode = getPara("id", "");
		set("ccode", ccode);

		String ctype = getPara("ctype", "");
		set("ctype", ctype);

		String path = getRequest().getContextPath();
		String webServer = getRequest().getScheme() + "://" + getRequest().getServerName() + ":"
				+ getRequest().getServerPort() + path;
		;
		set("webServer", webServer);
		render("system/sysorginfo_edit.html");
	}

	// 获取数据
	public void getData() {
		String ccode = getPara("ccode", "");
		String sql = "select * from org_info where ccode = '" + ccode + "'";
		Record record = Db.findFirst(sql);
		renderJson(record);
	}

	@Inject
	private SysDeptService sysDeptService;

	// 保存
	@Inject
	private UploadFileService uploadFileService;

	public void save() {
		UploadFile file = getFile("img_src");
		String logo_src = getPara("logo_src", "");

		String ctype = getPara("ctype", "");
		if (file == null && StrUtil.isEmpty(logo_src)) {
			renderJson(error("请选择上传图片"));
			return;
		}

		if (file != null) {
			Record recf = null;
			recf = uploadFileService.uploadFile(getResponse(), getRequest(), file, "/img/");
			if (recf != null) {
				String filename = recf.getStr("fname");
				String filetype = recf.getStr("fext");
				String filepath = recf.getStr("path");
				String filesize = recf.getStr("fsize");
				String fupname = recf.getStr("fname");
				String furl = recf.getStr("furl");
				logo_src = furl;
			}
		}

		String ccode = getPara("ccode", "");
		String cname = getPara("cname", "");
		String cproname = getPara("cproname", "");

		String address = getPara("address", "");
		String cmanager = getPara("cmanager", "");
		String cphone = getPara("cphone", "");
		String email = getPara("email", "");
		String limitstart = getPara("limitstart", "");
		String limitend = getPara("limitend", "");
		String cflag = getPara("cflag", "");
		String prefix = getPara("prefix", "");
		String ip_dcs = getPara("ip_dcs", "");
		String ip_das = getPara("ip_das", "");
		String db_type = getPara("db_type", "");
		String nmax_cd = getPara("nmax_cd", "0");

		String sql = "select * from org_info where ccode = '" + ccode + "'";
		Record record = Db.findFirst(sql);
		boolean isedit = true;
		if (record == null) {
			isedit = false;
			record = new Record();
			record.set("ccode", ccode);
			record.set("prefix", prefix);

			sql = "select max(ccode_auto) from org_info";
			String ccode_auto = DBHelper.GetString(sql);
			if (ccode_auto.isEmpty()) {
				ccode_auto = "0";
			}

			int code = Integer.valueOf(ccode_auto);
			code += 1;
			ccode_auto = SysUtil.getFillNum(String.valueOf(code), 2);

			record.set("ccode_auto", ccode_auto);

			record.set("create_by", ToolSession.getUser(this).getCname());
			record.set("create_time", DateUtils.getNowDate());
		} else {
			if (!ctype.equals("edit")) {
				renderJson(error("产权编号重复，请重新填写"));
				return;
			}
		}
		record.set("update_by", ToolSession.getUser(this).getCname());
		record.set("update_time", DateUtils.getNowDate());

		record.set("cname", cname);
		record.set("cproname", cproname);
		if (file != null) {
			record.set("logo_src", logo_src);
		}
		record.set("address", address);
		record.set("cmanager", cmanager);
		record.set("cphone", cphone);
		record.set("email", email);
		record.set("limitstart", limitstart);
		record.set("limitend", limitend);
		record.set("cflag", cflag);
		record.set("ip_dcs", ip_dcs);
		record.set("ip_das", ip_das);
		record.set("db_type", db_type);
		record.set("nmax_cd", nmax_cd);

		if (DbUtils.save("org_info", "ccode", record, isedit)) {
			if (!isedit) {
				sysDeptService.addOrg(ccode, cname, cphone, email, ccode + "admin", "1234");
				// 插入编码前缀的处理

			}
			renderJson(success("保存成功！"));
		} else {
			renderJson(success("保存失败！"));
		}

	}

	// 删除
	public void remove() {
		String ids = getPara("ids", "");
		ids = ToolString.getIdsToStr(ids);
		String sql = "delete from org_info where ccode in (" + ids + ")";
		if (Db.update(sql) > 0) {
			renderJson(success("删除成功！"));
		} else {
			renderJson(success("删除失败！"));
		}
	}
}
